package edu.rpi.bt.server.service.impl;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.photos.SearchParameters;

import edu.rpi.bt.server.service.FlickrSearchService;


@Transactional
public class FlickrSearchServiceImpl implements FlickrSearchService {
    private static final Logger log = Logger
    .getLogger(FlickrSearchServiceImpl.class);
    
    private Flickr flickr;
    private PhotosInterface photoInterface;
    private SearchParameters searchParameters;
    private REST rest;
    private static String apiKey = "fa5fe3b64297e9b5a8f878287605d38f";        // used to ID the application
    //private static String sharedSecret = "4b2ac9446928ffe3";    // used for signed calls
    private static String restHost = "www.flickr.com";
    String[] stringList;
    
    public String[] getImageUrls() {
        String[] imageUrls= {"http://edition.cnn.com/2000/LAW/08/17/bailey.battles.02/map.florida.orlando.jpg",
                "http://peacecorpsonline.org/messages/jpeg/vermont2aa.jpg",
                "http://static.howstuffworks.com/gif/travel/properties/494/san-francisco-city-guide-ga-1-1.jpg"
        };
        
        return imageUrls;
    }
    
    public String[] getFlickrSearch(String searchString, int num) {
        System.out.println("WE GOT TO FLICKR");
        searchParameters.setSort(SearchParameters.RELEVANCE);
        searchParameters.setText(searchString);
        try {
            rest = new REST();
            rest.setHost(restHost);
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        flickr = new Flickr(apiKey,rest);
        photoInterface = flickr.getPhotosInterface();
        
        try {
           PhotoList photoList = photoInterface.search(searchParameters, num, 1);
           if(photoList!=null){
               //Get search results and populate url list
               for(int i=0;i<photoList.size();i++){
                 this.stringList[i] = ((Photo)photoList.get(i)).getUrl();
             }
           }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FlickrException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return getImageUrls();
        //return this.stringList;
    }
  }